﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AnimaGestionPersonnage.Utilities;
using AnimaGestionPersonnage.Properties;

namespace AnimaGestionPersonnage.Vue
{
    public partial class FormCreateAdvDisadv : Form, ISkillBonusAdder
    {
        private AdvDisadv advDisadv = new AdvDisadv();
        private Boolean saved = true;

        public FormCreateAdvDisadv()
        {
            InitializeComponent();
        }

        public void ShowDialog(Form parent)
        {
            foreach (string s in Settings.Default.StatTypes)
            {
                comboBoxStats.Items.Add(s);
            }

            foreach (string s in Settings.Default.AdvDisadvTypes)
            {
                comboBoxTypeAdvDisadv.Items.Add(s);
            }

            base.ShowDialog(parent);
        }

        private void buttonAddBonusSkills_Click(object sender, EventArgs e)
        {
            new FormSkillModder().ShowDialog(this, Utils.RACE_BONUS);
        }

        private void buttonRemoveBonusSkill_Click(object sender, EventArgs e)
        {
            if (listBoxBonusSkills.SelectedItem != null)
            {
                advDisadv.modToSkill.RemoveAt(listBoxBonusSkills.SelectedIndex);
                listBoxBonusSkills.Items.RemoveAt(listBoxBonusSkills.SelectedIndex);
                dataChanged();
            }
        }

        private void buttonRemStatBonus_Click(object sender, EventArgs e)
        {
            if (listBoxStatBonuses.SelectedItem != null)
            {
                advDisadv.statBonuses.RemoveAt(listBoxStatBonuses.SelectedIndex);
                listBoxStatBonuses.Items.RemoveAt(listBoxStatBonuses.SelectedIndex);
                dataChanged();
            }
        }

        private void buttonAddStatBonus_Click(object sender, EventArgs e)
        {
            if (comboBoxStats.SelectedItem != null)
            {
                StatBonus tempBonus = new StatBonus((Stats)comboBoxStats.SelectedIndex, (int)numericUpDownStatBonus.Value);
                advDisadv.statBonuses.Add(tempBonus);
                listBoxStatBonuses.Items.Add(tempBonus.stat + " +" + tempBonus.bonus);
                dataChanged();
            }
        }

        private void ouvrirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            advDisadv = new FormEditionSelection().ShowDialog(this);
            if (advDisadv != null)
            {
                textBoxName.Text = advDisadv.name;
                numericUpDownCost.Value = advDisadv.cost;
                comboBoxTypeAdvDisadv.SelectedIndex = (int)advDisadv.type;
                richTextBoxNotes.Lines = advDisadv.notes.ToArray();
                foreach (SkillMod sm in advDisadv.modToSkill)
                {
                    listBoxBonusSkills.Items.Add(sm.skillName + " +" + sm.value);
                }
                foreach (StatBonus sb in advDisadv.statBonuses)
                {
                    listBoxStatBonuses.Items.Add(sb.stat + " +" + sb.bonus);
                }
            }
        }

        private void sauvegarderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Utils.advDisadvList.Contains(advDisadv))
            {
                Utils.advDisadvList.Remove(advDisadv);
            }
            Utils.advDisadvList.Add(advDisadv);

            Utils.xmlSerializeAdvDisadv(Settings.Default.FilePath + Settings.Default.AdvDisadv);
        }

        private void quitterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        public void addSkillBonus(SkillMod sm)
        {
            listBoxBonusSkills.Items.Add(sm.skillName + " +" + sm.value);
            advDisadv.modToSkill.Add(sm);
            dataChanged();
        }

        void dataChanged()
        {
            sauvegarderToolStripMenuItem.Enabled = true;
            saved = false;
        }

        private void richTextBoxNotes_TextChanged(object sender, EventArgs e)
        {
            advDisadv.notes.Clear();
            advDisadv.notes.AddRange(richTextBoxNotes.Lines);
            dataChanged();
        }

        private void textBoxName_TextChanged(object sender, EventArgs e)
        {
            advDisadv.name = textBoxName.Text;
            dataChanged();
        }

        private void comboBoxTypeAdvDisadv_SelectedIndexChanged(object sender, EventArgs e)
        {
            advDisadv.type = (AdvDisadvType)comboBoxTypeAdvDisadv.SelectedIndex;
            dataChanged();
        }

        private void numericUpDownCost_ValueChanged(object sender, EventArgs e)
        {
            advDisadv.cost = (int)numericUpDownCost.Value;
            dataChanged();
        }

        private void FormCreateAdvDisadv_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = Utils.onCloseEvent(saved, this, null,e);
        }
    }
}
